#ifndef MESSAGE_DECODE_BUFFER_H_
#define MESSAGE_DECODE_BUFFER_H_

#include "udp_fec_packet.h"

#include <assert.h>
#include <map>
#include <list>

class BlockDecodeBuffer;
typedef std::shared_ptr<BlockDecodeBuffer> SpBlockDecodeBuffer;

class MessageDecodeBuffer
{
public:
    MessageDecodeBuffer(int window_size);
    ~MessageDecodeBuffer();
    MessageList appendAndTryDecode(const char* buf, int len);

private:
    SpBlockDecodeBuffer addNewBlock(FecPacket* packet);
    uint64_t getMaxBlockId();
    uint64_t getMinBlockId();
    int full();
    SpBlockDecodeBuffer find(uint64_t blockid);
    void freeBlockBuffer(uint64_t  blockid);

private:
    size_t window_size_;
    std::map<uint64_t, SpBlockDecodeBuffer> buffs_;
    std::list<SpBlockDecodeBuffer> free_buffs_;
    size_t                      next_block_id_;
};
typedef std::shared_ptr<MessageDecodeBuffer> SpMessageDecodeBuffer;

#endif//MESSAGE_DECODE_BUFFER_H_